package com.gitee.wsl.ext.array



inline fun <T> Array<T>?.isBlank(): Boolean = isNullOrEmpty()

fun <T> Array<T?>.anyNull(): Boolean = any { it == null }

fun <T> Array<T?>.allNull(): Boolean = all { it == null }

fun <T> Array<Array<T?>>.anyNull(): Boolean = any { it.anyNull() }

fun <T> Array<Array<T?>>.allNull(): Boolean = all { it.allNull() }

inline fun <T> Array<Array<T>>.anyInner(predicate: (T) -> Boolean): Boolean = any { it.any(predicate) }

inline fun <T> Array<Array<T>>.allInner(predicate: (T) -> Boolean): Boolean = all { it.all(predicate) }


/**
 * UTF-8编码方式
 * ----------------------------------------------
 * 0xxxxxxx
 * 110xxxxx 10xxxxxx
 * 1110xxxx 10xxxxxx 10xxxxxx
 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 */
fun ByteArray.isUtf8() = byteType() == 100

/**
 * Checks if an element is included in an array
 * @param elem element to find in array
 * @param subSize size limit for subarray
 */
fun IntArray.inUntil(elem: Int, subSize: Int): Boolean {
    for (i in 0..<subSize) {
        if (get(i) == elem) return true
    }
    return false
}

/**
 * Checks if the contents of the list is the same as the given array.
 */
fun Array<*>.contentEquals(other: Array<*>): Boolean {
    if (size != other.size) return false
    for (i in indices) {
        if (get(i) != other[i]) return false
    }
    return true
}

/**
 * Checks if the contents of the list is the same as the given array.
 */
fun Array<*>.contentEquals(other: List<*>): Boolean {
    if (size != other.size) return false
    for (i in indices) {
        if (get(i) != other[i]) return false
    }
    return true
}

fun ByteArray.endsWith(subArray: ByteArray): Boolean{
    val startIndex = size - subArray.size
    return startIndex >= 0 && indexOf(
        subArray,
        startIndex,
        startIndex + 1
    ) == startIndex
}